package com.capgemini.fs.presentation.user.web.action;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.capgemini.fs.app.ekms.common.Constants;
import com.capgemini.fs.app.ekms.model.Group;
import com.capgemini.fs.app.ekms.model.Role;
import com.capgemini.fs.app.ekms.model.User;
import com.capgemini.fs.app.ekms.service.GroupService;
import com.capgemini.fs.app.ekms.service.RoleService;
import com.capgemini.fs.app.ekms.service.UserService;
import com.capgemini.fs.presentation.user.web.form.CreateUserForm;
public class CreateUserAction extends Action {

	private UserService		createUserService	= null;
	private RoleService		loginRoleService	= null;
	private GroupService	loginGroupService	= null;

	public UserService getCreateUserService() {
		return createUserService;
	}

	public void setCreateUserService(UserService createUserService) {
		this.createUserService = createUserService;
	}

	public GroupService getLoginGroupService() {
		return loginGroupService;
	}

	public void setLoginGroupService(GroupService loginGroupService) {
		this.loginGroupService = loginGroupService;
	}

	public RoleService getLoginRoleService() {
		return loginRoleService;
	}

	public void setLoginRoleService(RoleService loginRoleService) {
		this.loginRoleService = loginRoleService;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		CreateUserForm createUserForm = (CreateUserForm) form;

		// Decide userName is exit or not

		if (createUserService.getUserByName(createUserForm.getUsername()) != null) {
			request.setAttribute("Failed",
					"The \"User Name\" already exist, please fill in again!");
			request
					.setAttribute("createUserName", createUserForm
							.getUsername());
			request.setAttribute("createFirstName", createUserForm
					.getFirstname());
			request
					.setAttribute("createLastName", createUserForm
							.getLastname());
			request.setAttribute("createEmail", createUserForm.getEmail());
			request.setAttribute("createPhoneNo", createUserForm.getPhoneno());
			return mapping.findForward("fail");
		}
		if (!createUserForm.getPassword()
				.equals(createUserForm.getConfirmpwd())) {
			request
					.setAttribute(
							"Failed",
							"The \"Password\" and \"Confirm Password\" is not the same, please fill in again!");
			request
					.setAttribute("createUserName", createUserForm
							.getUsername());
			request.setAttribute("createFirstName", createUserForm
					.getFirstname());
			request
					.setAttribute("createLastName", createUserForm
							.getLastname());
			request.setAttribute("createEmail", createUserForm.getEmail());
			request.setAttribute("createPhoneNo", createUserForm.getPhoneno());
			return mapping.findForward("fail");
		}

		// add user
		User candidate = new User();
		candidate.setUserName(createUserForm.getUsername());
		candidate.setSecurityPassword(createUserForm.getPassword());
		candidate.setFirstName(createUserForm.getFirstname());
		candidate.setLastName(createUserForm.getLastname());
		candidate.setEmail(createUserForm.getEmail());
		// add category data
		
		if (createUserForm.getCategory() == "trainee"
				|| createUserForm.getCategory().equalsIgnoreCase("trainee")) {
			candidate.setMemberType(Constants.USER_MEMBERTYPE_TRAINEE);
		} else {
			candidate.setMemberType(Constants.USER_MEMBERTYPE_EMPLOYEE);

		}
		// add user status

		if (createUserForm.getActive() == 0) {

			candidate.setStatus(Constants.USER_STATUS_ACTIVE);
		} else {
			candidate.setStatus(Constants.USER_STATUS_INACTIVE);
		}
		// add phone number
		if ("".equals(createUserForm.getPhoneno())
				|| "" == createUserForm.getPhoneno()) {

		} else {
			candidate.setPhoneNo(createUserForm.getPhoneno());
		}
		// add role info
		Set<Role> roles = new HashSet<Role>();
		String[] role = createUserForm.getRole();
		System.out.println(role.length);
		for (int i = 0; i < role.length; i++) {
			System.out.println(role[i]);
			System.out.println(role[i]);
			System.out.println(role[i]);
			Role role1 = loginRoleService.getRoleByName(role[i]);
			roles.add(role1);
		}
		// add group info
		if (createUserForm.getGroup() == null) {

		} else {
			Set<Group> groups = new HashSet<Group>();
			String[] group = createUserForm.getGroup();
			for (int i = 0; i < group.length; i++) {
				Group group1 = loginGroupService.findGroupByGroupName(group[i]);
				groups.add(group1);

			}
			candidate.setGroups(groups);
		}
		// set attribute to UI
		// add Entry_Date
		if (createUserForm.getCategory() == "employee"
				|| createUserForm.getCategory().equalsIgnoreCase("employee")) {
			candidate.setEntryDate(new Date());
		}
		// add creator' user name
		HttpSession session = request.getSession();
		String creator = (String) session.getAttribute("username");
		candidate.setCreator(creator);

		try {

			candidate.setRoles(roles);

			createUserService.createUser(candidate);
			request.setAttribute("Success", "Created Successfully!");
			return mapping.findForward("suc");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("Failed", "Create Failed!");
			return mapping.findForward("fail");
		}

	}

}
